package com.neuedu.yyzx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.common.Ret;
import com.neuedu.yyzx.config.security.SecurityUtils;
import com.neuedu.yyzx.po.Backdown;
import com.neuedu.yyzx.po.Customer;
import com.neuedu.yyzx.po.User;
import com.neuedu.yyzx.service.BackdownService;
import com.neuedu.yyzx.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author
 * 项目：yyzx-project
 * site:http://...
 * 描述：退住管理
 * 思路：
 * @data 2025/4/23/9:58
 */
@RestController
@RequestMapping("/backdown")
public class BackdownController {
    @Autowired
    private BackdownService backdownService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    SecurityUtils securityUtils;

    // http://localhost:8080/outward/customer/page
    // 外出登记左侧 客户查询
    @GetMapping("customer/page")
    public Ret<List<Customer>> page(Page<Customer> page, Customer customer) {
        // 创建LambdaQueryWrapper对象，用于后续的条件构造
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();

        // 如果用户昵称不为空，则添加昵称模糊查询条件
        if (customer.getCustomerName() != null) {
            queryWrapper.like(Customer::getCustomerName, customer.getCustomerName());
        }
        if (customer.getRoomNo() != null) {
            queryWrapper.like(Customer::getRoomNo, customer.getRoomNo());
        }
        // 添加按创建时间降序排序的条件
        queryWrapper.orderByDesc(Customer::getCheckinDate);

        // 调用CustomerService的分页查询方法，并返回查询结果
        return Ret.ok(customerService.page(page, queryWrapper));
    }

    // http://localhost:8080/backdown/backList
    // 退住管理（右侧查询）
    @GetMapping("/backList")
    public Ret<Page<Backdown>> customerBack(Page<Backdown> page, Backdown backdown) {
        return Ret.ok(backdownService.customerBack(page, backdown));
    }

    // 添加退住申请
    // http://localhost:8080/backdown/saveOrUpdate
    @PostMapping("/saveOrUpdate")
    public Ret<Boolean> saveOrUpdate(@RequestBody Backdown backdown) {
        return Ret.ok(backdownService.saveOrUpdate(backdown));
    }
    // http://localhost:8080/backdown/getCustomerWithoutBack
    // 添加退住申请中的下拉选
    @GetMapping("/getCustomerWithoutBack")
    public Ret<List<Backdown>> getCustomerWithoutBack() {
        return Ret.ok(backdownService.getCustomerWithoutBack());
    }

    // 根据主键删除
    @DeleteMapping("/delete/{id}")
    public Ret<Boolean> removeById(@PathVariable int id) {
        return Ret.ok(backdownService.removeById(id));
    }

    // 修改退住审批状态 配合添加审批人和审批时间
    @PostMapping("/updateBackStatus/{backId}/{status}")
    public Ret<Boolean> updateBackStatus(@PathVariable int backId,@PathVariable int status) {
        User userInfo = securityUtils.getUserInfo();//当前登录用户，审核人员
        Date now = new Date();
        Backdown backdown = backdownService.getById(backId);

        System.out.println("now = " + now);
        System.out.println("userInfo = " + userInfo);
        backdown.setAuditperson(userInfo.getNickname());
        backdown.setAudittime(now);
        backdown.setAuditstatus(status);

        boolean timeWithPerson = backdownService.updateById(backdown);
        return Ret.ok(timeWithPerson);
    }

    // 根据主键查询
    // http://localhost:8080/backdown/getById/1
    @GetMapping("/getById/{id}")
    public Ret<Backdown> getById(@PathVariable int id) {
        return Ret.ok(backdownService.getById(id));
    }
}
